package com.xz.simplerpc.console.rest;

import com.xz.simplerpc.constants.Constants;
import com.xz.simplerpc.extension.URL;
import com.xz.simplerpc.registry.CuratorZookeeperClient;
import com.xz.simplerpc.registry.ZookeeperClient;
import com.xz.simplerpc.utils.CollectionUtils;
import com.xz.simplerpc.console.domain.Config;
import com.xz.simplerpc.console.domain.ProviderInfo;
import com.xz.simplerpc.console.domain.Result;
import lombok.extern.slf4j.Slf4j;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by fingal on 2018/1/14.
 */
@Path("/service")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Slf4j
public class SimpleRpcRest {
    private ZookeeperClient zkClient = new CuratorZookeeperClient(URL.valueOf(Config.getAddress()));

    private static final String ROOT = "/xz";

    @GET
    @Path("/list")
    public List<String> list() {
        return zkClient.getChildren(ROOT);
    }

    @GET
    @Path("/{service}/providers")
    public List<ProviderInfo> listProviders(@PathParam("service") @Encoded final String service) {
        List<String> list = zkClient.getChildren(ROOT + "/" + service + "/" + Constants.PROVIDERS_CATEGORY);
        List<ProviderInfo> providerInfos = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(list)) {
            for(String item : list) {
                URL url = URL.valueOf(URL.decode(item));
                ProviderInfo providerInfo = new ProviderInfo();
                if(url.hasParameter(Constants.DISABLED_KEY)) {
                    providerInfo.setAddress(url.removeParameter(Constants.DISABLED_KEY).toFullString());
                    providerInfo.setEnable(false);
                } else {
                    providerInfo.setAddress(URL.decode(url.toFullString()));
                    providerInfo.setEnable(true);
                }
                providerInfos.add(providerInfo);
            }
        }
        return providerInfos;
    }

    @GET
    @Path("/{service}/consumers")
    public List<String> listConsumers(@PathParam("service") @Encoded final String service) {
        List<String> list = zkClient.getChildren(ROOT + "/" + service + "/" + Constants.CONSUMERS_CATEGORY);
        List<String> result = new ArrayList<>();
        if(CollectionUtils.isNotEmpty(list)) {
            for(String item : list) {
                result.add(URL.valueOf(URL.decode(item)).getAddress());
            }
        }
        return result;
    }

    @POST
    @Path("/providers/status")
    public Result changeStatus(ProviderInfo providerInfo) {
        String path = ROOT + "/" + providerInfo.getService() + "/" + Constants.PROVIDERS_CATEGORY;
        URL url, oldUrl;
        url = oldUrl = URL.valueOf(providerInfo.getAddress());
        if (!providerInfo.getEnable()) {
            url = url.addParameter(Constants.DISABLED_KEY, true);
        } else {
            oldUrl = oldUrl.addParameter(Constants.DISABLED_KEY, true);
        }
        zkClient.delete(path + "/" + URL.encode(oldUrl.toFullString()));
        zkClient.create(path + "/" + URL.encode(url.toFullString()), true);

        return Result.DEFAULT;
    }
}
